//-----------------------------------------------------------------------
// <copyright file="GroupCollection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using Microsoft.Live.Core;

    /// <summary>
    /// Represents a collection of groups.
    /// </summary>
    /// <remarks>
    /// Groups provide the ability for users to organize their contacts by placing them into groups. Use <see cref="GroupCollection.Create"/> to create a new group.
    /// </remarks>
    public sealed class GroupCollection : Collection
    {
        internal GroupCollection(object obj, User user)
            : base(obj, user)
        {
        }

        /// <summary>
        /// Gets the maximum number of groups that a User can have in this collection.
        /// </summary>
        public int Capacity
        {
            get { return this.Entity.GetNumberProperty<int>("Capacity"); }
        }

        /// <summary>
        /// Creates a new group.
        /// </summary>
        /// <param name="groupName">The name of the group.</param>
        /// <returns>The group that was created.</returns>
        public Group Create(string groupName)
        {
            if (!IsNameValid(groupName))
            {
                throw new ArgumentException();
            }

            return this.User.GroupFactory.FindOrCreate(
                this.Entity.Invoke("Create", groupName));
        }

        /// <summary>
        /// Removes a group from the collection.
        /// </summary>
        /// <param name="group">The group to remove.</param>
        public void Remove(Group group)
        {
            this.Entity.Invoke("Remove", group.Entity);
        }

        /// <summary>
        /// Determines whether the provided group name is valid.
        /// </summary>
        /// <param name="groupName">The name to validate.</param>
        /// <returns><c>true</c> if the name is valid; <c>false</c> otherwise.</returns>
        public bool IsNameValid(string groupName)
        {
            return (bool)this.Entity.Invoke("IsNameValid", groupName);
        }

        /// <summary>
        /// Gets the <c>Group</c> by name.
        /// </summary>
        /// <param name="name">The name of the group.</param>
        /// <returns>The <c>Group</c> if it exists; otherwise <c>null</c>.</returns>
        public Group this[string name]
        {
            get
            {
                object obj = this.Entity.GetItem(name);
                if (obj != null)
                {
                    MessengerEntity entity = new MessengerEntity(obj);

                    return (Group)FindOrCreate(entity);
                }
                return null;
            }
        }

        internal override object FindOrCreate(MessengerEntity entity)
        {
            return this.User.GroupFactory.FindOrCreate(entity);
        }
    }
}